<?php
namespace app\wechat\controller;

use app\common\controller\WeChatBase;
use think\Config;
use think\Controller;
use think\Db;
use think\Session;
use app\common\model\AdminUser as AdminUserModel;

/**
 * 微信api
 * Class Api
 * @package app\wechat\controller
 */
class Api extends WeChatBase
{
    protected $AdminUserModel;

    protected function _initialize()
    {
        parent::_initialize();
        $this->AdminUserModel       = new AdminUserModel();
    }


    public function index(){
        // 从项目实例中得到服务端应用实例。
        $server = $this->app->server;

        // 根据获取到的消息作不同的处理
        $server->setMessageHandler(function($message){

            switch ($message->MsgType) {
                case 'event':
                    // return '收到事件消息';
                    return $this->_event($message);
                    break;
                case 'text':
                    return '收到文字消息';
                    break;
                case 'image':
                    return '收到图片消息';
                    break;
                case 'voice':
                    return '收到语音消息';
                    break;
                case 'video':
                    return '收到视频消息';
                    break;
                case 'location':
                    return '收到坐标消息';
                    break;
                case 'link':
                    return '收到链接消息';
                    break;
                // ... 其它消息
                default:
                    return '收到其它消息';
                    break;
            }
        });

        $server->serve()->send();

    }


    // 事件
    public function _event($message){


        $openid = $message->FromUserName;

        switch ($message->Event) {
            case 'subscribe':
                $userservice = $this->app->user;
                $userinfo = $userservice->get($openid);
                $name = $userinfo->nickname;

                $Quser = $this->AdminUserModel->where('openid',$openid)->find();
                if($Quser){
                    $Quser->status = 1;
                    $Quser->save();
                }else{
                    // 设置带参数的二维码
                    if($message->EventKey){
                        $p1_openid = str_replace('qrscene_','',$message->EventKey);
                        // 查询 , 修改
                        $p1_user = $this->AdminUserModel->where('openid',$p1_openid)->find();
                        // 写入三级关系数据
                        $this->AdminUserModel->p1 = $p1_user->id;
                        $this->AdminUserModel->p2 = $p1_user->p1;
                        $this->AdminUserModel->p3 = $p1_user->p2;
                    }

                    $this->AdminUserModel->username = $name;
                    $this->AdminUserModel->openid = $openid;
                    $this->AdminUserModel->create_time = time();
                    $this->AdminUserModel->status = 1;
                    $this->AdminUserModel->save();
                }

                wechat_qr($openid);
                return " 欢迎关注shifu204！！ !";
                break;
            case 'unsubscribe':
                $Quser = $this->AdminUserModel->where('openid',$openid)->find();
                if($Quser){
                    $Quser->status = 0;
                    $Quser->save();
                }
                break;
            case 'SCAN':
                break;
            case 'LOCATION':
                break;
            case 'CLICK':
                break;
            case 'VIEW':
                break;
            default:
                # code...
                break;
        }
    }



}
